package com.dingqb.myapplication.paint.shader;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.dingqb.myapplication.R;

/**
 * 着色器
 */
public class CircleView extends View {
    public CircleView(Context context) {
        super(context);
    }

    public CircleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    Paint paint = new Paint();
    Bitmap mBitmap;
    BitmapShader bitmapShader;


    void init() {
        mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.zhanglanlan);
        bitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Matrix matrix = new Matrix();
        float scale = getWidth() / mBitmap.getWidth();
        Log.e(TAG, "onDraw:scale " + scale);
        matrix.setScale(scale, scale);
        bitmapShader.setLocalMatrix(matrix);
        paint.setShader(bitmapShader);
        float half = getWidth() / 2;
        canvas.drawCircle(half, half, getWidth() / 2, paint);
    }

    public static final String TAG = "dingqb";
}